using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.Application.Contracts.Customized.RoomSourceVerifiy.Dtos.FastVerify;
using SOEI.Solution.Application.Contracts.Customized.RoomSourceVerifiy.Dtos.PropertyInfo;
using SOEI.Solution.DomainEntities.Parameters.DomainService;
using SOEI.Solution.DomainEntities.RoomSourceVerifiy.DomainService;
using SOEI.Solution.DomainEntities.RoomSourceVerifiy.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.RoomSourceVerifiy
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：房源核验基础数据服务<br/>
    /// 【创建日期】  ：2020.05.21 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = true)]
    public class RoomVerifyAppService : SolutionAppServiceBase, IRoomVerifyAppService
    {
        /// <summary>
        ///【RoomVerify】仓储层
        /// </summary>
        private readonly IRepository<RoomVerify, long> _roomverifyRepository;

        /// <summary>
        ///【RoomVerify】领域服务
        /// </summary>
        private readonly IRoomVerifyManager _roomverifyManager;

        private readonly IParameterManager _parameterManager;

        public RoomVerifyAppService(
            IRepository<RoomVerify, long> roomverifyRepository,
            IRoomVerifyManager roomverifyManager,
            IParameterManager parameterManager
        )
        {
            _roomverifyRepository = roomverifyRepository;
            _roomverifyManager = roomverifyManager;
            _parameterManager = parameterManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【RoomVerify】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<PagedResultDto<RoomVerifyListDto>> GetPaged(GetRoomVerifysInput input)
        {
            var query = _roomverifyManager.QueryEntityListAsNoTracking()
                          //var query = _roomverifyRepository.GetAll()
                          //模糊搜索 字段CertificateCode
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CertificateCode.Contains(input.FilterText))
                          //模糊搜索 字段CertificateName
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.CertificateName.Contains(input.FilterText))
                          //模糊搜索 字段StateCode
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.StateCode.Contains(input.FilterText))
                          //模糊搜索 字段Remark
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Remark.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<RoomVerifyListDto>>(entityList);

            return new PagedResultDto<RoomVerifyListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【RoomVerify】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<RoomVerifyListDto> GetById(EntityDto<long> input)
        {
            var entity = await _roomverifyRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<RoomVerifyListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【RoomVerify】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetRoomVerifyForEditOutput> GetForEdit(NullableIdDto<long> input)
        {
            var output = new GetRoomVerifyForEditOutput();
            RoomVerifyEditDto editDto;

            if (input.Id.HasValue)
            {
                var entity = await _roomverifyRepository.GetAsync(input.Id.Value);
                editDto = ObjectMapper.Map<RoomVerifyEditDto>(entity);
            }
            else
            {
                editDto = new RoomVerifyEditDto();
            }
            output.RoomVerify = editDto;
            return output;
        }
        /// <summary>
        ///【RoomVerify】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task CreateOrUpdate(CreateOrUpdateRoomVerifyInput input)
        {

            if (input.RoomVerify.Id.HasValue)
            {
                await Update(input.RoomVerify);
            }
            else
            {
                await Create(input.RoomVerify);
            }
        }
        /// <summary>
        ///【RoomVerify】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<RoomVerifyEditDto> Create(RoomVerifyEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<RoomVerify>(input);
            //调用领域服务
            entity = await _roomverifyManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<RoomVerifyEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【RoomVerify】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task Update(RoomVerifyEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id.Value;
            var entity = await _roomverifyRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _roomverifyManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【RoomVerify】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _roomverifyManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【RoomVerify】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(List<long> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _roomverifyManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/

        /// <summary>
        ///【RoomVerify】 获取快速核验编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetFastVerifyForEditOutput> GetForFastVerifyEdit(NullableIdDto<long> input)
        {
            var output = new GetFastVerifyForEditOutput();

            if (input.Id.HasValue)
            {
                var entity = await _roomverifyRepository.GetAsync(input.Id.Value);
                output.FastVerify = ObjectMapper.Map<FastVerifyEditDto>(entity);
            }
            else
            {
                output.FastVerify = new FastVerifyEditDto();
                output.FastVerify.FastVerifyForm = new FastVerifyForm();
                output.FastVerify.FastVerifyFormParams = new FastVerifyFormParams();
                output.FastVerify.FastVerifyFormParams.CertificateTypeParamList = _parameterManager.QueryCertificateType().ToList();
                output.FastVerify.FastVerifyFormParams.PropertyTypeParamList = _parameterManager.QueryPropertyType().ToList();
            }
            return output;
        }


        /// <summary>
        ///【RoomVerify】 获取产权信息编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetPropertyInfoForEditOutput> GetForPropertyInfoEdit(NullableIdDto<long> input)
        {
            var output = new GetPropertyInfoForEditOutput();
            PropertyInfoEditDto editDto;

            if (input.Id.HasValue)
            {
                var entity = await _roomverifyRepository.GetAsync(input.Id.Value);
                editDto = ObjectMapper.Map<PropertyInfoEditDto>(entity);
            }
            else
            {
                editDto = new PropertyInfoEditDto();
            }
            output.PropertyInfo = editDto;
            return output;
        }
        #endregion
    }
}
